package com.umeox.watch.audiobooks.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.umeox.watch.audiobooks.R;
import com.umeox.watch.audiobooks.utils.StoryManager;
import com.yc.music.model.AudioBean;
import com.yc.music.service.PlayService;
import com.yc.music.tool.BaseAppHelper;

import java.util.ArrayList;
import java.util.List;

public class AudiobooksAdapter extends RecyclerView.Adapter<AudiobooksAdapter.Holder> {

    private final Context mContext;
    private final LayoutInflater mInflater;
    private int mLayoutId;
    private AnimationDrawable playingAnim;
    private String curAudioId = "";

    public void setCurAudioId(String id) {
        curAudioId = id;
    }

    private List<AudioBean> mAudioBookList = new ArrayList<>();
    private OnClickListener mOnClickListener;

    public AudiobooksAdapter(Context context, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = mInflater.inflate(mLayoutId, parent, false);
        return new Holder(itemView);
    }


    @Override
    public void onBindViewHolder(Holder holder, int position) {

        if (mAudioBookList.isEmpty()) {
            return;
        }

        final AudioBean item = mAudioBookList.get(position);
        holder.bind(mContext, item);
        holder.itemView.setOnClickListener(
                v -> {
                    if (mOnClickListener != null) {
                        mOnClickListener.onItemClick(item);
                    }
                });

    }

    @Override
    public int getItemCount() {
        return mAudioBookList.size();
    }


    public List<AudioBean> getAudioInfos() {
        return mAudioBookList;
    }

    public void setStories(List<AudioBean> audioBeans) {
        mAudioBookList.clear();
        mAudioBookList.addAll(audioBeans);
        notifyDataSetChanged();
    }


    class Holder extends RecyclerView.ViewHolder {
        TextView mStoryName;
        TextView mStoryAuthor;
        ImageView mIvFavorite;
        ImageView mIvPlaying;

        public Holder(final View itemView) {
            super(itemView);
            mStoryName = itemView.findViewById(R.id.tv_story_name);
            mStoryAuthor = itemView.findViewById(R.id.tv_story_author);
            mIvFavorite = itemView.findViewById(R.id.iv_favorite);
            mIvPlaying = itemView.findViewById(R.id.iv_playing);
        }

        public void bind(Context mContext, AudioBean item) {
            mIvFavorite.setSelected(StoryManager.INSTANCE.getFavoriteNameList().contains(item.getId()));
            playingAnim = (AnimationDrawable) mIvPlaying.getDrawable();
            if (item.getId().equals(curAudioId)) {
                mIvPlaying.setVisibility(View.VISIBLE);
                PlayService playService = BaseAppHelper.get().getMusicService();
                if (playingAnim.isRunning() && (playService != null && playService.isPausing())) {
                    playingAnim.stop();
                }

                if (!playingAnim.isRunning() && (playService != null && playService.isPlaying())) {
                    playingAnim.start();
                }
            } else {
                if (playingAnim.isRunning()) {
                    playingAnim.stop();
                }
                mIvPlaying.setVisibility(View.INVISIBLE);
            }
            mStoryName.setText(item.getTitle());
//            mStoryName.setText(item.getId());
            mStoryAuthor.setText(item.getArtist());

            mIvFavorite.setOnClickListener(v -> mIvFavorite.setSelected(StoryManager.INSTANCE.setFavorite(item)));

        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onItemClick(AudioBean item);
    }
}
